﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BiblioWeb.DataAccess;
using BiblioWeb.Models;

namespace BiblioWeb.Controllers
{
    public class ReservaController : Controller
    {
        private ContextoBiblio db;
        private ReservaRepo reservaRepo;

        public ReservaController()
        {
            this.db = new ContextoBiblio();
            this.reservaRepo = new ReservaRepo(db);
        }
        //
        // GET: /Reserva/

        public ActionResult Index()
        {
            return View(this.reservaRepo.Todos());
        }

        //
        // GET: /Reserva/Details/5

        public ActionResult Details(int id = 0)
        {
            Reserva reserva =this.reservaRepo.Obtener(id);
            if (reserva == null)
            {
                return HttpNotFound();
            }
            return View(reserva);
        }

        //
        // GET: /Reserva/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Reserva/Create

        [HttpPost]
        public ActionResult Create(Reserva reserva)
        {
            if (ModelState.IsValid)
            {
                this.reservaRepo.Agregar(reserva);
                return RedirectToAction("Index");
            }

            return View(reserva);
        }

        //
        // GET: /Reserva/Edit/5

        public ActionResult Edit(int id = 0)
        {
            Reserva reserva = this.reservaRepo.Obtener(id);
            if (reserva == null)
            {
                return HttpNotFound();
            }
            return View(reserva);
        }

        //
        // POST: /Reserva/Edit/5

        [HttpPost]
        public ActionResult Edit(Reserva reservaModificada)
        {
            if (ModelState.IsValid)
            {
                Reserva reservaActual = this.reservaRepo.Obtener(reservaModificada.IdReserva);
                //tengo que ver bien que voy a modificar
                this.reservaRepo.Modificar(reservaActual);
                return RedirectToAction("Index");
            }
            return View(reservaModificada);
        }

        //
        // GET: /Reserva/Delete/5

        public ActionResult Delete(int id = 0)
        {
            Reserva reserva =this.reservaRepo.Obtener(id);
            if (reserva == null)
            {
                return HttpNotFound();
            }
            return View(reserva);
        }

        //
        // POST: /Reserva/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            this.reservaRepo.Eliminar(id);
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}